package com.qdlc.p2p.biz.service.lottery.impl;

import java.util.List;

import javax.annotation.Resource;

import com.qdlc.p2p.biz.processor.event.LotteryScoreValueEvent;
import com.qdlc.p2p.biz.service.lottery.AbstractDrawLotteryService;
import com.qdlc.p2p.biz.service.lottery.LotteryAlgorithmFactory;
import com.qdlc.p2p.biz.zdata.LotteryResult;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.constant.LotteryCategory;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.BeanCopierUtils;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dao.*;
import com.qdlc.p2p.dal.dto.*;

/**
 * Created by hongbl on 2016/2/2.
 */
public class ScoreDrawLotteryService implements AbstractDrawLotteryService {
    /** 积分抽奖 */
    private static final String SCORE_LOTTERY = "score_lottery";

    @Resource
    private ScoreDao scoreDao;

    @Resource
    private ScoreTypeDao scoreTypeDao;

    @Resource
    private LotteryDao lotteryDao;

    @Resource
    private ScoreLotteryRecordDao scoreLotteryRecordDao;

    @Resource
    private LotteryRecordDao lotteryRecordDao;

    /**
     * 抽奖过程 将从前分步的做法现在合成一步
     * 1 进行抽奖资格的验证
     * 2 进行抽奖
     * 3 颁奖以及记录
     */
    public LotteryResult process(User user, int category){
        //用户信息验证
        if (user == null) {
            throw new UserException("用户不存在!", BusinessException.TYPE_JSON);
        }
        //获取用户积分信息
        Score userScore = scoreDao.findByUserId(user.getUserId());
        //获取抽奖所需的积分
        int needScore= scoreTypeDao.getScoreTypeByNid(SCORE_LOTTERY).getValue();
        if (userScore.getValidScore() < needScore) {
            throw new UserException("您当前可用积分不足!", BusinessException.TYPE_JSON);
        }
        //取抽奖规则
        List<Lottery> lotteryConfig = lotteryDao.getLotteryByCategory(category);
        //根据抽奖配置以及算法得到结果
        LotteryResult lotteryResult = LotteryAlgorithmFactory.getResult(lotteryConfig);
        //消费积分以及将抽奖放入队列中执行

        //积分抽奖记录表记录
        ScoreLotteryRecord scoreLotteryRecord = new ScoreLotteryRecord();
        scoreLotteryRecord.setUser(user);
        scoreLotteryRecord.setType(lotteryResult.getType());
        scoreLotteryRecord.setValue(lotteryResult.getValue());
        scoreLotteryRecord.setAddTime(DateUtil.getNow());
        scoreLotteryRecord.setAddIp(Global.getIP());
        scoreLotteryRecordDao.save(scoreLotteryRecord);
        //抽奖记录表
        LotteryRecord lotteryRecord = new LotteryRecord();
        BeanCopierUtils.copyProperties(lotteryResult, lotteryRecord);
        lotteryRecord.setCategory(LotteryCategory.SCORE.getCategory());
        lotteryRecord.setUserId(user.getUserId());
        lotteryRecord.setAddTime(DateUtil.getNow());
        lotteryRecordDao.save(lotteryRecord);

        //积分抽奖放入队列
        LotteryScoreValueEvent lotteryScoreValueEvent = new LotteryScoreValueEvent();
        lotteryScoreValueEvent.setUser(user);
        lotteryScoreValueEvent.setLotteryResult(lotteryResult);
        QueueHandlers.submit(lotteryScoreValueEvent);
        return lotteryResult;
    }
}
